# -*- coding: utf-8 -*-
# @Time: 2021/12/19 11:48
# @Author: 胡志远
# @Software: PyCharm
import sys

from PyQt5.QtCore import pyqtSlot
from PyQt5.QtGui import QPixmap
from PyQt5.QtWidgets import QDialog, QApplication, QMessageBox, QTableWidgetItem

from Model import modelController
from UI.MainWindow import Ui_Dialog


class ControlApp(QDialog):
    def __init__(self):
        super(ControlApp, self).__init__()
        self._ui = Ui_Dialog()
        self._ui.setupUi(self)

        self._model = modelController()

        self._ui.pushButton.clicked.connect(self.startRunning)

        self._model.isFinish_signal.connect(self.restartAgain)

    @pyqtSlot()
    def startRunning(self):
        """
        开始跑程序
        :return: 无
        """
        self._ui.pushButton.setEnabled(False)

        self._model.typeOfChilli = 1 if self._ui.radioButton.isChecked() else 2
        self._model.num = self._ui.spinBox.value()

        self._model.start()

    @pyqtSlot(object)
    def restartAgain(self, val):
        # 清空表格和图片
        self._ui.tableWidget.setRowCount(0)
        self._ui.label_3.setText("图片将显示在这里")
        if val:
            # 图片
            self._ui.label_3.setPixmap(QPixmap(val["pic"]))
            # 表格
            index = 0
            for key, value in val["dic"].items():
                # 设置行数
                self._ui.tableWidget.setRowCount(self._ui.tableWidget.rowCount()+1)
                # 增加内容
                self._ui.tableWidget.setItem(index, 0, QTableWidgetItem(str(key)))
                self._ui.tableWidget.setItem(index, 1, QTableWidgetItem(str(value)))
                index += 1
        else:
            QMessageBox(QMessageBox.Warning, '警告', '没查到！').exec_()

        self._ui.pushButton.setEnabled(True)


if __name__ == '__main__':
    app = QApplication(sys.argv)
    window = ControlApp()
    window.show()
    app.exec_()
